﻿using Utils.Wpf;

namespace Plugins.UserControls
{
	public class StockParametersSelectorVM : ViewModelValidatedBase
	{
		#region Properties

		public StockParametersSelectorData Data { get; private set; }

		public decimal PercentageFee
		{
			get
			{
				return this.Data.PercentageFee;
			}
			set
			{
				if (this.Data.PercentageFee != value)
				{
					this.Data.PercentageFee = value;
					base.OnPropertyChanged("PercentageFee");
				}
			}
		}

		public decimal FixedFeePerItem
		{
			get
			{
				return this.Data.FixedFeePerItem;
			}
			set
			{
				if (this.Data.FixedFeePerItem != value)
				{
					this.Data.FixedFeePerItem = value;
					base.OnPropertyChanged("FixedFeePerItem");
				}
			}
		}

		public decimal FixedFeePerOperation
		{
			get
			{
				return this.Data.FixedFeePerOperation;
			}
			set
			{
				if (this.Data.FixedFeePerOperation != value)
				{
					this.Data.FixedFeePerOperation = value;
					base.OnPropertyChanged("FixedFeePerOperation");
				}
			}
		}

		public decimal FixedReserve
		{
			get
			{
				return this.Data.FixedReserve;
			}
			set
			{
				if (this.Data.FixedReserve != value)
				{
					this.Data.FixedReserve = value;
					base.OnPropertyChanged("FixedReserve");
				}
			}
		}

		public decimal PercentageReserve
		{
			get
			{
				return this.Data.PercentageReserve;
			}
			set
			{
				if (this.Data.PercentageReserve != value)
				{
					this.Data.PercentageReserve = value;
					base.OnPropertyChanged("PercentageReserve");
				}
			}
		}

		public decimal ValueMultiplier
		{
			get
			{
				return this.Data.ValueMultiplier;
			}
			set
			{
				if (this.Data.ValueMultiplier != value)
				{
					this.Data.ValueMultiplier = value;
					base.OnPropertyChanged("ValueMultiplier");
				}
			}
		}

		#endregion

		#region Public Methods

		public StockParametersSelectorVM()
		{
			this.Data = new StockParametersSelectorData();
			this.Data.PropertyChanged +=Data_PropertyChanged; // Local object
		}

		protected override string ValidateField(string fieldName)
		{
			string result = null;

			if (fieldName == "PercentageFee")
			{
				if (this.PercentageFee < 0)
				{
					base.ValidatedErrors[fieldName] = true;
					result = "The fees cannot be smaller than 0";
				}
			}
			else if (fieldName == "FixedFeePerItem")
			{
				if (this.FixedFeePerItem < 0)
				{
					base.ValidatedErrors[fieldName] = true;
					result = "The fees cannot be smaller than 0";
				}
			}
			else if (fieldName == "FixedFeePerOperation")
			{
				if (this.FixedFeePerOperation < 0)
				{
					base.ValidatedErrors[fieldName] = true;
					result = "The fees cannot be smaller than 0";
				}
			}
			else if (fieldName == "FixedReserve")
			{
				if (this.FixedReserve < 0)
				{
					base.ValidatedErrors[fieldName] = true;
					result = "The fixed reserve cannot be smaller than one";
				}
			}
			else if (fieldName == "PercentageReserve")
			{
				if (this.PercentageReserve < 0)
				{
					base.ValidatedErrors[fieldName] = true;
					result = "The percentage reserve cannot be smaller than one";
				}
			}
			else if (fieldName == "ValueMultiplier")
			{
				if (this.ValueMultiplier < 1)
				{
					base.ValidatedErrors[fieldName] = true;
					result = "The value multiplier cannot be smaller than one";
				}
			}

			return result;
		}

		#endregion

		#region Event Handlers

		private void Data_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			base.OnPropertyChanged(e.PropertyName);
		}

		#endregion
	}
}
